<template>
  <div class="reportFormPage" v-if="show">
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">{{ this.form.baobmc }}</div>
        <div class="codeText">
          <tr v-for="(item, index) in form.baobbhList">
            {{
              item
            }}
          </tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>报告编号：</span>
          <span class="placeholder">{{ form.baogbh }}</span>
        </div>
      </div>
      <!-- .........................1111111111111111111111111111111......................................................... -->
      <div class="table-border">
        <table height="220px" class="top-table">
          <tbody>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程名称</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcmc }}</span>
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程部位/用途</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>

                <span>；样品数量：</span>
                <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
                <span>；来样时间：</span>
                <span :class="{ placeholder: !form.yangpsj }">{{ form.yangpsj }}</span>
              </td>
            </tr>
            <tr>
              <td height="25" align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验检测日期</span>
              </td>
               <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验条件</span>
              </td>
              <td v-if="isPdf" width="35%">
                温度：
                <span style="display: inline-block; width: 10%">{{ form.shiytjwd }}</span>
                ℃；湿度：
                <span style="display: inline-block; width: 10%">{{ form.shiytjsd }}</span
                >%RH
              </td>
              <td v-else width="35%">
                温度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjwd" class="height100" style="width: 15%" />℃；湿度：
                <el-input :type="isPdf ? 'string' : 'number'" v-model="form.shiytjsd" class="height100" style="width: 15%" />%RH
              </td>
            </tr>
            <tr>
              <td height="40" align="center" width="15%">
                <span style="width: 90px; display: inline-block">检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark" @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="50" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark" @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="610px" class="middle-table">
          <tbody>
            <tr>
              <td rowspan="2" width="6%">构件号</td>
              <td colspan="4" height="25px">
                <span>钢筋间距</span>
              </td>
              <td colspan="4">
                <span>保护层厚度</span>
              </td>
              <td rowspan="2">备注</td>
            </tr>
            <tr>
              <td height="25px">设计值（mm）</td>
              <td>实测点数</td>
              <td>合格点数</td>
              <td>合格率（%）</td>
              <td>设计值（mm）</td>
              <td>实测点数</td>
              <td>合格点数</td>
              <td>合格率（%）</td>
            </tr>
            <tr v-for="(item, index) in form.detailList" :key="index">
              <td>{{ item.goujh }}</td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.gangjSjz"
                  @blur="clearEmpty(index, 'gangjSjz')"
                  @keydown.native="handleKeyDown($event, index, 0)"
                  :ref="`${index}_0`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.gangjScds"
                  @blur="clearEmpty(index, 'gangjScds')"
                  @keydown.native="handleKeyDown($event, index, 1)"
                  :ref="`${index}_1`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.gangjHgds"
                  @blur="clearEmpty(index, 'gangjHgds')"
                  @keydown.native="handleKeyDown($event, index, 2)"
                  :ref="`${index}_2`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.gangjHgl"
                  @blur="clearEmpty(index, 'gangjHgl')"
                  @keydown.native="handleKeyDown($event, index, 3)"
                  :ref="`${index}_3`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.baohcSjz"
                  @blur="clearEmpty(index, 'baohcSjz')"
                  @keydown.native="handleKeyDown($event, index, 4)"
                  :ref="`${index}_4`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.baohcScds"
                  @blur="clearEmpty(index, 'baohcScds')"
                  @keydown.native="handleKeyDown($event, index, 5)"
                  :ref="`${index}_5`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.baohcHgds"
                  @blur="clearEmpty(index, 'baohcHgds')"
                  @keydown.native="handleKeyDown($event, index, 6)"
                  :ref="`${index}_6`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.baohcHgl"
                  @blur="clearEmpty(index, 'baohcHgl')"
                  @keydown.native="handleKeyDown($event, index, 7)"
                  :ref="`${index}_7`"
                />
              </td>
              <td>
                <el-input
                  :type="isPdf ? 'string' : 'number'"
                  tabindex="200"
                  v-model="item.remark"
                  @blur="clearEmpty(index, 'remark')"
                  @keydown.native="handleKeyDown($event, index, 8)"
                  :ref="`${index}_8`"
                />
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="100px" class="bottom-table">
          <tr>
            <td style="border-bottom-width: 1px">
              <span v-if="isPdf" class="textarea-span">{{ form.jiancjl }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.jiancjl" @focus="handleFocus('jiancjl')" @dblclick.native="getJcjl()" />
            </td>
          </tr>
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left">
              <!-- {{this.form.jiancr}} -->
            </td>
            <td width="10%" align="right">审核：</td>
            <td width="15%" align="left">
              <!-- {{this.form.jilr}} -->
            </td>
            <td width="10%" align="right">批准：</td>
            <td align="left" width="15%">
              <!-- {{this.form.fuhr}} -->
            </td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>

<script>
import decimal from "@/utils/big-decimal";
import Bus from "@/utils/bus";
import { handleFocus, emptyConvert, uniqueStr, handleKeyDown } from "@/views/reportForms/utils";
import { getHntjgGjbhcbgBsInfo } from "@/api/report_hntjg/hntjg";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";
import BaQjDetailView from "@/views/sycs/baQjDetail/BaQjDetailView.vue";
import TableLineLogChart from "@/views/reportForms/components/TableLineLogChart.vue";

export default {
  name: "gjbhcbg",
  components: {
    BsInstrumentView,
    BsJiancyjView,
    BsJudgeBaseView,
    BaQjDetailView,
    TableLineLogChart
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      form: {
        detailList: [],
        baobbhList: []
      },
      show: false
    };
  },

  watch: {
    isPdf(newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created() {
    this.init();
  },
  mounted() {
    // 检测依据
    Bus.$on("setGjbhcBgJiancyj", (data) => {
      const { jiancyj, jiancyjIds } = data;
      const str = jiancyj + (this.form.jiancyj ? "、" + this.form.jiancyj : "");
      const ids = jiancyjIds + (this.form.jiancyjIds ? "," + this.form.jiancyjIds : "");
      this.form.jiancyj = uniqueStr(str, "、");
      this.form.jiancyjIds = uniqueStr(ids, ",");
    });
    // 主要设备
    Bus.$on("setGjbhcBgZhuyyqsbmcjbh", (data) => {
      const { zhuyyqsbmcjbh, shebIds } = data;
      const str = zhuyyqsbmcjbh + (this.form.zhuyyqsbmcjbh ? "、" + this.form.zhuyyqsbmcjbh : "");
      const ids = shebIds + (this.form.shebIds ? "," + this.form.shebIds : "");
      this.form.zhuyyqsbmcjbh = uniqueStr(str, "、");
      this.form.shebIds = uniqueStr(ids, ",");
    });
    Bus.$on("setGjbhcBgData", (info) => {
      for (const key in info) {
        if (Object.prototype.hasOwnProperty.call(info, key)) {
          this.form.detailList[0][key] = info[key];
        }
      }
      this.calculateHgl(0, "gangj");
      this.calculateHgl(0, "baohc");
    });
    // 传值试验条件
    Bus.$on("setGjbhcBgShiytj", (data) => {
      const { shiytjwd, shiytjsd } = data;
      this.form.shiytjwd = shiytjwd;
      this.form.shiytjsd = shiytjsd;
    });
  },
  beforeDestroy() {
    // 停止监听事件
    Bus.$off("setGjbhcBgJiancyj");
    Bus.$off("setGjbhcBgZhuyyqsbmcjbh");
    Bus.$off("setGjbhcBgData");
  },
  methods: {
    // 根据报表id获取当前报表信息
    init() {
      this.show = false;
      getHntjgGjbhcbgBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        this.setInitialValue();
        if (!this.form.detailList || this.form.detailList.length <= 0) {
          this.getDetailList();
        }
        this.show = true;
        if (!this.form.jiancjl) {
          this.form.jiancjl = "检测结论：";
        }
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
      });
    },
    // 设置初始值
    setInitialValue() {
      if (this.form) {
        this.form.baobmc = this.form.baobmc || "钢筋位置、保护层厚度试验检测报告";
        this.form.baobbh = this.form.baobbh || "BGLP02004F";
        this.$set(this.form, "baobbhList", []);
        this.form.baobbhList = ["BGLP02004F"];
      }
    },
    getDetailList() {
      this.$set(this.form, "detailList", []);
      for (let i = 0; i < 15; i++) {
        const d1 = {
          detailId: undefined,
          excelId: this.excelId,
          remark: undefined,
          sort: i + 1,
          goujh: i + 1 + "",
          gangjSjz: undefined,
          gangjScds: undefined,
          gangjHgds: undefined,
          gangjHgl: undefined,
          baohcSjz: undefined,
          baohcScds: undefined,
          baohcHgds: undefined,
          baohcHgl: undefined
        };
        this.form.detailList.push(d1);
      }
    },
    // 显示检测依据列表
    showBsJiancyjView() {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj(info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
      Bus.$emit("setGjbhcBgJiancyj", { jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds });
    },
    // 显示判定依据列表
    showBsJudgeBaseView() {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge(info) {
       this.form.pandyjIds = info.ids;
      this.form.pandyj = info.label;
    },
    // 显示仪器列表
    showBsInstrumentView() {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument(info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
      Bus.$emit("setGjbhcBgZhuyyqsbmcjbh", { zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds });
    },
    // 计算合格率
    calculateHgl(index, type) {
      let hgl = null;
      let Scds = this.form.detailList[index][type + "Scds"];
      let Hgds = this.form.detailList[index][type + "Hgds"];
      if (Scds && Hgds) {
        hgl = decimal.divide(decimal.multiply(Hgds, 100), Scds);
      }
      this.form.detailList[index][type + "Hgl"] = hgl;

      if (!this.form.detailList[index][type + "Scds"]) {
        this.form.detailList[index][type + "Scds"] = null;
      }
      if (!this.form.detailList[index][type + "Hgds"]) {
        this.form.detailList[index][type + "Hgds"] = null;
      }
    },
    // 值为空设为null
    clearEmpty(index, type) {
      if (!this.form.detailList[index][type]) {
        this.form.detailList[index][type] = null;
      }
    },
    getJcjl() {
      let jcjl = "检测结论：经检测，";
      jcjl = jcjl + (this.form.yangpmc || "");
      let a1, a2;
      if (this.form.detailList[0].gangjHgl) {
        a1 = this.form.detailList[0].gangjHgl;
      }
      if (this.form.detailList[0].baohcHgl) {
        a2 = this.form.detailList[0].baohcHgl;
      }
      jcjl = jcjl + "的钢筋间距合格率为" + a1 + "%，钢筋保护层厚度合格率为" + a2 + "%。";
      this.form.jiancjl = jcjl;
    },
    submitForm() {
      return this.form;
    },
    handleFocus(event) {
      handleFocus(event, this.page, this.form[event]);
    },
    handleKeyDown(event, rowIndex, colIndex) {
      const maxArr = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
      const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
      if (this.$refs[ref]?.length) {
        this.$refs[ref][0].focus();
      } else if (this.$refs[ref]) {
        this.$refs[ref].focus();
      }
    }
  }
};
</script>

<style scoped></style>
